/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import core.ErrorLogger;
import core.Patient;
import core.Recipes;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JFrame;
import javax.swing.JTable;
import util.JTableUtil;

/**
 *
 * @author Marcin
 */
public class PatientForm extends javax.swing.JFrame {
    private int patientID;
    private int visitID;
    /**
     * Creates new form PatientForm
     */
    public PatientForm(int visitID,int patientID,boolean visit) {
        try {
            initComponents();
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            ResultSet rs = Patient.getPatient(patientID);
            this.patientID = patientID;
            this.visitID = visitID;
            if(rs.next())
            {
                lName.setText(lName.getText()+""+rs.getString("IMIE"));
                lSurname.setText(lSurname.getText()+""+rs.getString("NAZWISKO"));
                lEmail.setText(lEmail.getText()+""+rs.getString("E-MAIL"));
                lPesel.setText(lPesel.getText()+""+rs.getString("PESEL"));
                lAdress.setText(lAdress.getText()+""+rs.getString("ADRES"));
                lNumber.setText(lNumber.getText()+""+rs.getString("NUMER UBEZPIECZENIA"));
            }
            else {
                dispose();
            }
            bRecept.setVisible(visit);
            bZalecenia.setVisible(visit);
        } catch (SQLException ex) {
            ErrorLogger.getInstance().log(ex.getMessage(), true, null);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        lName = new javax.swing.JLabel();
        lSurname = new javax.swing.JLabel();
        lEmail = new javax.swing.JLabel();
        lAdress = new javax.swing.JLabel();
        lPesel = new javax.swing.JLabel();
        lNumber = new javax.swing.JLabel();
        bHistory = new javax.swing.JButton();
        scPanel = new javax.swing.JScrollPane();
        bRecept = new javax.swing.JButton();
        bZalecenia = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Przychodnia");

        jLabel1.setText("Informacje o pacjencie:");

        lName.setText("Imię:");

        lSurname.setText("Nazwisko:");

        lEmail.setText("Email:");

        lAdress.setText("Adres:");

        lPesel.setText("Pesel:");

        lNumber.setText("Numer ubezpieczenia:");

        bHistory.setText("Pokaż historie");
        bHistory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bHistoryActionPerformed(evt);
            }
        });

        bRecept.setText("Dodaj recepte");
        bRecept.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bReceptActionPerformed(evt);
            }
        });

        bZalecenia.setText("Zalecenia");
        bZalecenia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bZaleceniaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lName)
                            .addComponent(lSurname)
                            .addComponent(lEmail))
                        .addGap(207, 207, 207)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lNumber)
                            .addComponent(lPesel)
                            .addComponent(lAdress))))
                .addGap(0, 269, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(bHistory, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(bRecept, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE)
                    .addComponent(bZalecenia, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(scPanel))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lName)
                    .addComponent(lAdress))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lSurname)
                    .addComponent(lPesel))
                .addGap(5, 5, 5)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lEmail)
                    .addComponent(lNumber))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(bHistory)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bRecept)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bZalecenia)
                        .addGap(0, 239, Short.MAX_VALUE))
                    .addComponent(scPanel)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bHistoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bHistoryActionPerformed
        final JTable tmp = JTableUtil.createTable(Patient.getPatientHistory(patientID));
        tmp.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    if (e.getClickCount() == 2) {
                        JTable target = (JTable) e.getSource();
                        int row = target.getSelectedRow();
                        scPanel.setViewportView(Recipes.getRecipesOListByWizytaID(Integer.parseInt(tmp.getValueAt(row, 0).toString())));
                    }
                }
            });
        scPanel.setViewportView(tmp);
    }//GEN-LAST:event_bHistoryActionPerformed

    private void bReceptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bReceptActionPerformed
        scPanel.setViewportView(new Recept_panel(visitID));
    }//GEN-LAST:event_bReceptActionPerformed

    private void bZaleceniaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bZaleceniaActionPerformed
        scPanel.setViewportView(new recommendationsPanel(visitID));
    }//GEN-LAST:event_bZaleceniaActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bHistory;
    private javax.swing.JButton bRecept;
    private javax.swing.JButton bZalecenia;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lAdress;
    private javax.swing.JLabel lEmail;
    private javax.swing.JLabel lName;
    private javax.swing.JLabel lNumber;
    private javax.swing.JLabel lPesel;
    private javax.swing.JLabel lSurname;
    private javax.swing.JScrollPane scPanel;
    // End of variables declaration//GEN-END:variables
}
